package com.servlet;

import com.dao.imp.FindAddress;
import com.entity.Address;
import net.sf.json.JSONArray;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

/**
 * @author Mathison
 * @Title: ${NAME}
 * @Package: ${PACKAGE_NAME}
 * @Description: TODO
 * @date 2021/11/25 20:46
 **/

@WebServlet(name = "GetAddressListServlet", value = "/GetAddressListServlet")
public class GetAddressListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        int user_id = Integer.parseInt(session.getAttribute("userId").toString());

        String sql = "SELECT * FROM address WHERE user_id="+user_id;

        List<Address> addressList = FindAddress.getAddress(sql);

        JSONArray json = JSONArray.fromObject(addressList);
        response.getWriter().print(json);

    }
}
